<?php

if ( ! function_exists('d3downloads_rss_new') ) {
	function d3downloads_rss_new($mydirname, $limit=0, $offset=0, $cid=0)
	{
		$db =& Database::getInstance() ;
		$myts =& MyTextSanitizer::getInstance();
		require_once 'common_functions.php' ;
		if ( $cid ) $where_cid .= " AND d.cid = ".( int )$cid;
		$whr_cat4read = "d.cid IN (".implode(",",d3download_can_read( $mydirname ) ).")" ;
		$sql = "SELECT d.lid, d.cid, d.title, d.description, d.html, d.smiley, d.br, d.xcode, d.submitter, d.date, d.hits, d.rating, d.votes, d.comments, c.title AS ctitle FROM ".$db->prefix( $mydirname."_downloads" )." d LEFT JOIN ".$db->prefix( $mydirname."_cat" )." c ON d.cid=c.cid WHERE d.visible = '1' AND ( $whr_cat4read ) ".$where_cid." ORDER BY d.date DESC";

		$result = $db->query( $sql, $limit, $offset );

		$URL_MOD = XOOPS_URL."/modules/".$mydirname;

		$i = 0;
		$ret = array();

		while( $row = $db->fetchArray($result))
		{
			$id     = intval( $row['lid'] );
			$catid  = intval( $row['cid'] );
			$submitter  = intval( $row['submitter'] );
			$ret[$i]['link']     = $URL_MOD."/index.php?page=singlefile&amp;cid=".$catid."&amp;lid=".$id;
			$ret[$i]['cat_link'] = $URL_MOD."/index.php?cid=".$catid;
			$ret[$i]['title']    = $myts->makeTboxData4Show( $row['title'] );
			$ret[$i]['cat_name'] = $myts->makeTboxData4Show( $row['ctitle'] );
			$ret[$i]['time'] = intval( $row['date'] );
			$ret[$i]['uid']  = $submitter;
			$ret[$i]['hits'] = intval( $row['hits'] );
			$ret[$i]['id']   = $id;
// replies
			$ret[$i]['replies'] = intval( $row['comments'] );

// description

			$html = intval( $row['html'] );
			$smiley = intval( $row['smiley'] );
			$xcode = intval( $row['xcode'] );
			$br = intval( $row['br'] );
			$body = $myts->displayTarea( $row['description'] , $html, $smiley, $xcode, 1, $br ) ;
			if ( strstr ( $body , '[pagebreak]' ) ){
				$desc_string = explode( '[pagebreak]', $body , 2 ) ;
				$body4assign = $desc_string[0];
			} else {
				$body4assign = $body ;
			}
			$ret[$i]['description'] = $body4assign;

			$i++;
		}

		return $ret;
	}
}

if (! function_exists('d3downloads_make_context')) {
	function d3downloads_make_context( $text, $words=array(), $l=255 ) {
		static $strcut = "";
		if (!$strcut)
			$strcut = create_function ( '$a,$b,$c', (function_exists('mb_strcut'))?
				'return mb_strcut($a,$b,$c);':
				'return strcut($a,$b,$c);');
	
		$text = str_replace(array('&lt;','&gt;','&amp;','&quot;','&#039;'),array('<','>','&','"',"'"),$text);
	
		if (!is_array($words)) $words = array();
	
		$ret = "";
		$q_word = str_replace(" ","|",preg_quote(join(' ',$words),"/"));
	
		$match = array();
		if (preg_match("/$q_word/i",$text,$match)) 	{
			$ret = ltrim(preg_replace('/\s+/', ' ', $text));
			list($pre, $aft) = array_pad(preg_split("/$q_word/i", $ret, 2), 2, "");
			$m = intval($l/2);
			$ret = (strlen($pre) > $m)? "... " : "";
			$ret .= $strcut($pre, max(strlen($pre)-$m+1,0),$m).$match[0];
			$m = $l-strlen($ret);
			$ret .= $strcut($aft, 0, min(strlen($aft),$m));
			if (strlen($aft) > $m) $ret .= " ...";
		}
	
		if (!$ret) {
			$ret = $strcut($text, 0, $l);
			$ret = preg_replace('/&([^;]+)?$/', '', $ret);
		}
	
		return htmlspecialchars( $ret, ENT_NOQUOTES );
	}
}

?>